package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.TextAttribute;
import java.util.Map;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;
import javax.swing.text.JTextComponent;

import vcp.client.GUI.events.ValidationDescriptionEvent;
import vcp.client.GUI.mvc.controller.UIRootController;
import vcp.client.GUI.mvc.view.custom.CustomInputVerifier;
import vcp.client.GUI.mvc.view.effects.ValidationLayerUI;
import vcp.client.interfaces.listeners.IValidationListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.RegexStrings;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.enums.PopUpMessageType;
/**
 * <p> user interface Log in screen view class </p>
 * @author VCP team
 *
 */
public class UILoginView extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton btnCancel;
	private JButton btnSubmit;
	private JFormattedTextField ftfUser;
	private JPasswordField ftfPassword;
	private JLabel lblForgotPassword;

	private IValidationListener validationListener;
	/**
	 * setter
	 * @param validationListener
	 */
	public void setValidationListener ( IValidationListener validationListener )
	{
		this.validationListener = validationListener;
	}

	private ValidationLayerUI idValidationLayerUI;
	/**
	 * constructor
	 */
	public UILoginView ()
	{
		super();

		this.resetView();
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	protected void setupLayout ()
	{
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(100, 10));
		this.add(panel, BorderLayout.WEST);

		JPanel panel_1 = new JPanel();
		panel_1.setPreferredSize(new Dimension(100, 10));
		this.add(panel_1, BorderLayout.EAST);

		JPanel panel_2 = new JPanel();
		this.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3);
		panel_3.setLayout(new GridLayout(3, 0, 0, 0));

		JPanel panel_9 = new JPanel();
		panel_3.add(panel_9);

		JPanel panel_10 = new JPanel();
		panel_3.add(panel_10);
		panel_10.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel_11 = new JPanel();
		panel_10.add(panel_11);
		panel_11.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblNewLabel = new JLabel("User ID:");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_11.add(lblNewLabel);
		lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 32));

		this.ftfUser = new JFormattedTextField();
		this.ftfUser.setToolTipText("Format:##...##");
		JLayer <JTextComponent> idLayer = new JLayer <JTextComponent>(this.ftfUser);
		this.idValidationLayerUI = new ValidationLayerUI();
		idLayer.setUI(this.idValidationLayerUI);
		panel_11.add(idLayer);
		this.ftfUser.setPreferredSize(new Dimension(200, 50));
		this.ftfUser.setFont(new Font("Lucida Grande", Font.PLAIN, 32));

		JPanel panel_12 = new JPanel();
		panel_10.add(panel_12);
		panel_12.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblNewLabel_1 = new JLabel("Password:");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_12.add(lblNewLabel_1);
		lblNewLabel_1.setFont(new Font("Lucida Grande", Font.BOLD, 32));

		this.ftfPassword = new JPasswordField();
		panel_12.add(this.ftfPassword);
		this.ftfPassword.setPreferredSize(new Dimension(200, 50));
		this.ftfPassword.setFont(new Font("Lucida Grande", Font.PLAIN, 32));

		JPanel panel_13 = new JPanel();
		panel_3.add(panel_13);
		panel_13.setLayout(new GridLayout(2, 0, 0, 0));

		this.lblForgotPassword = new JLabel(Common.LBL_FORGOT_PASSWORD);
		this.lblForgotPassword.setHorizontalAlignment(SwingConstants.CENTER);
		panel_13.add(this.lblForgotPassword);
		this.lblForgotPassword.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.lblForgotPassword.setForeground(new Color(65, 105, 225));
		this.lblForgotPassword.setFont(new Font("Lucida Grande", Font.PLAIN, 22));

		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4);
		panel_4.setLayout(new GridLayout(4, 0, 0, 0));

		JPanel panel_5 = new JPanel();
		panel_4.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_7 = new JPanel();
		FlowLayout fl_panel_7 = (FlowLayout) panel_7.getLayout();
		fl_panel_7.setAlignment(FlowLayout.RIGHT);
		panel_5.add(panel_7);

		this.btnCancel = new JButton(Common.BTN_CANCEL);
		panel_7.add(this.btnCancel);
		this.btnCancel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnCancel.setFocusable(false);
		this.btnCancel.setName(Common.BTN_CANCEL);
		this.btnCancel.setPreferredSize(new Dimension(175, 75));
		this.btnCancel.setFont(new Font("Lucida Grande", Font.PLAIN, 16));

		JPanel panel_6 = new JPanel();
		panel_5.add(panel_6);

		JPanel panel_8 = new JPanel();
		FlowLayout fl_panel_8 = (FlowLayout) panel_8.getLayout();
		fl_panel_8.setAlignment(FlowLayout.LEFT);
		panel_5.add(panel_8);

		this.btnSubmit = new JButton(Common.BTN_SUBMIT);
		panel_8.add(this.btnSubmit);
		this.btnSubmit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnSubmit.setName(Common.BTN_SUBMIT);
		this.btnSubmit.setPreferredSize(new Dimension(175, 75));
		this.btnSubmit.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
	}

	@Override
	protected void setupListeners ()
	{
		this.btnCancel.addActionListener(this);
		this.btnSubmit.addActionListener(this);

		this.ftfUser.addKeyListener(new KeyListener()
		{

			@Override
			public void keyTyped ( KeyEvent e )
			{
			}

			@Override
			public void keyReleased ( KeyEvent e )
			{
				String fieldUserText = UILoginView.this.ftfUser.getText();
				String fieldPasswordText = String.valueOf(UILoginView.this.ftfPassword.getPassword());

				boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(fieldUserText, fieldPasswordText);
				UILoginView.this.btnSubmit.setEnabled(result);
			}

			@Override
			public void keyPressed ( KeyEvent e )
			{
			}
		});
		this.ftfPassword.addKeyListener(new KeyListener()
		{

			@Override
			public void keyTyped ( KeyEvent e )
			{
			}

			@Override
			public void keyReleased ( KeyEvent e )
			{
				String fieldUserText = UILoginView.this.ftfUser.getText();
				String fieldPasswordText = String.valueOf(UILoginView.this.ftfPassword.getPassword());

				boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(fieldUserText, fieldPasswordText);
				UILoginView.this.btnSubmit.setEnabled(result);
			}

			@Override
			public void keyPressed ( KeyEvent e )
			{
			}
		});

		this.lblForgotPassword.addMouseListener(new MouseAdapter()
		{
			private Font originalFont;

			@SuppressWarnings (
					{ "unchecked", "rawtypes" } )
			@Override
			public void mouseEntered ( MouseEvent e )
			{
				this.originalFont = e.getComponent().getFont();
				Map attributes = this.originalFont.getAttributes();
				attributes.put(TextAttribute.UNDERLINE,
						TextAttribute.UNDERLINE_ON);
				e.getComponent().setFont(
						this.originalFont.deriveFont(attributes));
			}

			@Override
			public void mouseExited ( MouseEvent e )
			{
				e.getComponent().setFont(this.originalFont);
			}
		});

		Pattern idPattern = UIRootController.DEBUG
				? Pattern.compile(RegexStrings.ACCEPT_ALL)
						: Pattern.compile(RegexStrings.ID_10_DIGITS);
				this.ftfUser.setInputVerifier(new CustomInputVerifier(this.idValidationLayerUI, idPattern));
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBMIT, 45, 45);
		if ( icon != null )
		{
			this.btnSubmit.setIcon(icon);
			this.btnSubmit.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CANCEL, 45, 45);
		if ( icon != null )
		{
			this.btnCancel.setIcon(icon);
			this.btnCancel.setIconTextGap(iconTextGap);
		}
	}

	@Override
	public void clearView ()
	{
		this.ftfUser.setText("");
		this.ftfPassword.setText("");
	}

	@Override
	public void resetView ()
	{
		this.btnSubmit.setEnabled(false);
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton loginViewButton = (JButton) actionEvent.getSource();
		switch ( loginViewButton.getName() )
		{
			case Common.BTN_SUBMIT:
				if ( this.validationListener != null )
				{
					if ( this.ftfUser.getInputVerifier().verify(this.ftfUser) )
					{
						Integer loginID = Integer.parseInt(this.ftfUser.getText());
						String password = String.valueOf(this.ftfPassword.getPassword());
						this.validationListener.onValidationRequest(new ValidationDescriptionEvent(this, loginID, password));
					}
					else
					{
						String infoMessage = String.format(UserNotification.NOT_CORRECT_FORMAT_FOR__VALUE__, this.ftfUser.getToolTipText());
						this.controllerDelegate.showPopUpMessage(infoMessage, PopUpMessageType.INFORMATION);
					}
				}
				break;
			case Common.BTN_CANCEL:
				this.controllerDelegate.onButtonPress(null);
				break;
		}
	}

}
